﻿/**********************************************************************
*创建人：蔡水明
*创建时间：2018/12/13 19:03:15
*说明：<Function>
/**********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public class HttpHelper
    {
        /// <summary>
        /// HttpClient实现Post请求(异步)
        /// </summary>
        public static async Task<string> DoPost(string url, Dictionary<string, string> data)
        {
            string result = "";
            //设置HttpClientHandler的AutomaticDecompression
            var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            //创建HttpClient（注意传入HttpClientHandler）
            using (var http = new HttpClient(handler))
            {
                //使用FormUrlEncodedContent做HttpContent
                var content = new FormUrlEncodedContent(data);
                //await异步等待回应
                var response = await http.PostAsync(url, content);
                //确保HTTP成功状态值
                response.EnsureSuccessStatusCode();
                //await异步读取最后的JSON（注意此时gzip已经被自动解压缩了，因为上面的AutomaticDecompression = DecompressionMethods.GZip）
                result = await response.Content.ReadAsStringAsync();
                //Console.WriteLine(await response.Content.ReadAsStringAsync());
            }
            return result;
        }
        /// <summary>
        /// HttpClient实现Get请求(异步)
        /// </summary>
        public static async Task<string> DoGet(string url)
        {
            string result = "";
            //创建HttpClient（注意传入HttpClientHandler）
            var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            using (var http = new HttpClient(handler))
            {
                try
                {
                    //await异步等待回应
                    var response = await http.GetAsync(url);
                    //确保HTTP成功状态值
                    response.EnsureSuccessStatusCode();

                    //await异步读取最后的JSON（注意此时gzip已经被自动解压缩了，因为上面的AutomaticDecompression = DecompressionMethods.GZip）
                    result = await response.Content.ReadAsStringAsync();
                    //Console.WriteLine(await response.Content.ReadAsStringAsync());
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
                
            }
            return result;
        }
        /// <summary>
        /// HttpClient实现Put请求(异步)
        /// </summary>
        public static async void DoPut()
        {
            var userId = 1;
            string url = "http://localhost:52824/api/register?userid=" + userId;

            //设置HttpClientHandler的AutomaticDecompression
            var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            //创建HttpClient（注意传入HttpClientHandler）
            using (var http = new HttpClient(handler))
            {
                //使用FormUrlEncodedContent做HttpContent
                var content = new FormUrlEncodedContent(new Dictionary<string, string>(){
                    { "Name","修改zzl"},
                    { "Info", "Put修改动作"}//键名必须为空
                });

                //await异步等待回应

                var response = await http.PutAsync(url, content);
                //确保HTTP成功状态值
                response.EnsureSuccessStatusCode();
                //await异步读取最后的JSON（注意此时gzip已经被自动解压缩了，因为上面的AutomaticDecompression = DecompressionMethods.GZip）
                Console.WriteLine(await response.Content.ReadAsStringAsync());
            }
        }

     

    }
}
